<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/2
 * Time: 16:39
 */

namespace app\controllers;

use yii;
use app\models\Inland;
use \app\service\InlandService;

class InlandController extends ApiBaseController
{
    public function actionSplit()
    {
        if (yii::$app->request->isPost) {
            $id = yii::$app->request->post('id');
            $invoice = yii::$app->request->post('invoice');
            $fee = yii::$app->request->post('total_fee');
            //复制一条记录
            if (!is_numeric($id) || $id < 0) {
                return \json_encode(['code' => 0, 'msg' => '没有数据1']);
            }
            $model = new Inland();
            $list = $model->one(['id' => $id]);
            if (!$list) {
                return json_encode(['code' => 0, 'msg' => '没有数据']);
            }
            $list['pid'] = $id;
            $list['invoice'] = $invoice;
            $list['total_fee'] = $fee;
            unset($list['id']);
            $re = $model->insert($list);

            if ($re) {
                //校验金额一致
                $service = new InlandService();
                $service->checkMoneySame($id);
                echo json_encode(['code' => 1, 'msg' => '添加成功']);
            } else {
                echo json_encode(['code' => '添加失败', 'msg' => '请重试']);
            }
        }
    }


    public function actionUpdate()
    {
        if (!yii::$app->request->isPost) {
            return \json_encode(['code' => 0, 'msg' => 'not allowed']);
        }
        $id = yii::$app->request->post('id');
        $model = new Inland();
        $info = $model->one(['id' => $id]);
        if ($info['lock_status'] == 1) {
            return json_encode(['code' => 0, 'msg' => '记录已被锁定无法修改']);
        }
        $data = [];
        $column = yii::$app->request->post('column');
        $value = yii::$app->request->post('value');
        $data[$column] = $value;
        $data['last_modify'] = $this->user['job_number'];
        $data['last_modify_time'] = time();
        $re = $model->save($data, ['id' => $id]);
        if ($re) {
            $service = new InlandService();
            $service->checkMoneySame($info['pid']);
            echo json_encode(['code' => 1, 'msg' => '更新成功']);
        } else {
            echo json_encode(['code' => 0, 'msg' => '更新失败，请重试']);
        }

    }
}